package com.ruoyi.web.controller.snmp;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SnmpMemory;
import com.ruoyi.system.service.ISnmpMemoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 *  Controller
 *
 * @author ruoyi
 * @date 2025-02-05
 */
@RestController
@RequestMapping("/snmp/memory")
public class SnmpMemoryController extends BaseController
{
    @Autowired
    private ISnmpMemoryService snmpMemoryService;

    /**
     * 查询 列表
     */
    @PreAuthorize("@ss.hasPermi('snmp:memory:list')")
    @GetMapping("/list")
    public TableDataInfo list(SnmpMemory snmpMemory)
    {
        startPage();
        List<SnmpMemory> list = snmpMemoryService.selectSnmpMemoryList(snmpMemory);
        return getDataTable(list);
    }

    /**
     * 导出 列表
     */
    @PreAuthorize("@ss.hasPermi('snmp:memory:export')")
    @Log(title = "", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SnmpMemory snmpMemory)
    {
        List<SnmpMemory> list = snmpMemoryService.selectSnmpMemoryList(snmpMemory);
        ExcelUtil<SnmpMemory> util = new ExcelUtil<SnmpMemory>(SnmpMemory.class);
        util.exportExcel(response, list, " 数据");
    }

    /**
     * 获取 详细信息
     */
    @PreAuthorize("@ss.hasPermi('snmp:memory:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(snmpMemoryService.selectSnmpMemoryById(id));
    }
    @GetMapping(value = "/byip/{ip}")
    public AjaxResult getInfobyIP(@PathVariable("ip") String ip) throws Exception {
        SnmpMemory snmpMemoryentity=new SnmpMemory();
        snmpMemoryentity= snmpMemoryService.GetMemWithOne(ip);
        if(snmpMemoryentity==null||snmpMemoryentity.getIp().length()<1){
            snmpMemoryentity=snmpMemoryService.GetInfo(ip);
        }
        return success(snmpMemoryentity);
    }

        /**
         * 新增
         */
    @PreAuthorize("@ss.hasPermi('snmp:memory:add')")
    @Log(title = "", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SnmpMemory snmpMemory)
    {
        return toAjax(snmpMemoryService.insertSnmpMemory(snmpMemory));
    }

    /**
     * 修改
     */
    @PreAuthorize("@ss.hasPermi('snmp:memory:edit')")
    @Log(title = " ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SnmpMemory snmpMemory)
    {
        return toAjax(snmpMemoryService.updateSnmpMemory(snmpMemory));
    }

    /**
     * 删除
     */
    @PreAuthorize("@ss.hasPermi('snmp:memory:remove')")
    @Log(title = "", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(snmpMemoryService.deleteSnmpMemoryByIds(ids));
    }
}
